package com.example.androiddemo.gallery;
/**  
 * GalleryAdapter.java
 * @version 1.0
 * @author Haven
 * @createTime 2011-12-9 下午05:04:34
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.example.androiddemo.R;
import com.example.androiddemo.entities.MediaInfo;
import com.example.androiddemo.grid.GridViewAndRelateLayoutActivity;
import com.example.androiddemo.utils.ImageUtils;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;


public class PictureShowAdapter extends BaseAdapter
{
	private static final String TAG = "PictureShowAdapter";
	
	private Activity mContext;
	
	/** 图片信息列表 */
	private List<MediaInfo> mList = null;

	
	public class ViewHolder
	{
		public ZoomImageView zoomImageView;
		public ProgressBar progressBar;
	}
	
	public PictureShowAdapter(Activity context, List<MediaInfo> pictureList)
	{
		this.mContext = context;
		this.mList = pictureList;
	}

	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position;
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;
		if (null == convertView)
		{
			View view = mContext.getLayoutInflater().inflate(R.layout.layout_picture_show_adapter, null);
			holder = new ViewHolder();
			holder.zoomImageView = (ZoomImageView)view.findViewById(R.id.zoomImageView);
			holder.progressBar = (ProgressBar)view.findViewById(R.id.pbWaitting);
			view.setTag(holder);
			convertView = view;
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}
		
		holder.progressBar.setVisibility(View.VISIBLE);
		ZoomImageView zoomImageView = holder.zoomImageView;
		zoomImageView.setProgressBar(holder.progressBar);
		Bitmap bitmap = null;
		String url = mList.get(position).getUrl();			
		if(mList.get(position).getType()==MediaInfo.IMG){
			bitmap = getBitmapFromUrl(url);
		}else if(mList.get(position).getType()==MediaInfo.RECORD){
			Resources res=mContext.getResources();  
			Drawable drawable = res.getDrawable(R.drawable.yuyin);  
			//实际上这是一个BitmapDrawable对象  
			BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;  
			//可以在调用getBitmap方法，得到这个位图  
			bitmap=bitmapDrawable.getBitmap();  
		}else if(mList.get(position).getType()==MediaInfo.VIDEO){
			bitmap = getVideoThumbnail(mList.get(position).getUrl(),480,480,  
	                MediaStore.Images.Thumbnails.MICRO_KIND);
		}
		holder.zoomImageView.setImageBitmap(bitmap);
		return convertView;
	}
	/** 
     * 获取视频的缩略图 
     * 先通过ThumbnailUtils来创建一个视频的缩略图，然后再利用ThumbnailUtils来生成指定大小的缩略图。 
     * 如果想要的缩略图的宽和高都小于MICRO_KIND，则类型要使用MICRO_KIND作为kind的值，这样会节省内存。 
     * @param videoPath 视频的路径 
     * @param width 指定输出视频缩略图的宽度 
     * @param height 指定输出视频缩略图的高度度 
     * @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。 
     *            其中，MINI_KIND: 512 x 384，MICRO_KIND: 96 x 96 
     * @return 指定大小的视频缩略图 
     */  
    private Bitmap getVideoThumbnail(String videoPath, int width, int height,  
            int kind) {  
        Bitmap bitmap = null;  
        // 获取视频的缩略图  
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);  
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,  
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);  
        return bitmap;  
    }  
	private Bitmap getBitmapFromUrl(String url){
		Bitmap bitmap = null;
		Bitmap newBitmap = null; 
		bitmap = GridViewAndRelateLayoutActivity.gridviewBitmapCaches.get(url);
		if(bitmap != null){
			return bitmap;
			
		}
//		url = url.substring(0, url.lastIndexOf("/"));//处理之前的路径区分，否则路径不对，获取不到图片
		
		//bitmap = BitmapFactory.decodeFile(url);
		//这里我们不用BitmapFactory.decodeFile(url)这个方法
		//用decodeFileDescriptor()方法来生成bitmap会节省内存
		//查看源码对比一下我们会发现decodeFile()方法最终是以流的方式生成bitmap
		//而decodeFileDescriptor()方法是通过Native方法decodeFileDescriptor生成bitmap的
		
		try {
			FileInputStream is = new FileInputStream(url);
			bitmap = BitmapFactory.decodeFileDescriptor(is.getFD());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		newBitmap = ThumbnailUtils.extractThumbnail(bitmap,  
				480,480);
//		ImageUtils.getBitmapThumbnail(bitmap,width,height);
		bitmap.recycle();
		return newBitmap;
	}
}
